package com.npy.reggie.contorller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npy.reggie.doman.Dish;
import com.npy.reggie.doman.DishFlavor;
import com.npy.reggie.doman.R;
import com.npy.reggie.dto.DishDto;
import com.npy.reggie.service.CategoryService;
import com.npy.reggie.service.DishFlavorService;
import com.npy.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService ;

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;


    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){
        log.info("dishDto{}",dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("ennnn");
    }
    @GetMapping("/page")
    public R<Page<DishDto>> page(int page , int pageSize , String name){

        Page<Dish> pageTypeInfo = new Page<>(page,pageSize);
        Page<DishDto> pageInfo = new Page<>();

        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().like(name!=null, Dish::getName,name).orderByDesc(Dish::getCreateTime);

        dishService.page(pageTypeInfo,queryWrapper);

        BeanUtils.copyProperties(pageTypeInfo,pageInfo,"records");

        log.info("page dish{}",pageInfo.getRecords().toString());

        List<DishDto> dishDtos = pageTypeInfo.getRecords().stream().map(item-> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            dishDto.setCategoryName(categoryService.getById(item.getCategoryId()).getName());
            return dishDto;
        }).collect(Collectors.toList());

        pageInfo.setRecords(dishDtos);

        return R.success(pageInfo);
    }
    @GetMapping("/{id}")
    public R<DishDto> selectById(@PathVariable Long id){
        Dish dish = dishService.getById(id);
        DishDto dishDto = new DishDto();

        List<DishFlavor> dishFlavors = dishFlavorService.list(new QueryWrapper<DishFlavor>().
                lambda().eq(dish.getId() != null, DishFlavor::getDishId, dish.getId()));

        BeanUtils.copyProperties(dish,dishDto);

        dishDto.setFlavors(dishFlavors);

        return R.success(dishDto);
    }
    
    @PutMapping
    public R<String> updateById(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("闲的？");
    }

    @GetMapping("/list")
    public R<List<DishDto>> listR(Long categoryId){
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(categoryId!=null, Dish::getCategoryId,categoryId);

        List<DishDto> dishDtos = dishService.list(queryWrapper).stream().map(itme->{
            DishDto dishDto = new DishDto();



            dishDto.setFlavors(dishFlavorService.list(
                    new QueryWrapper<DishFlavor>().lambda()
                            .eq(itme.getId()!=null,DishFlavor::getDishId,itme.getId())));

            BeanUtils.copyProperties(itme,dishDto);

            return dishDto;

        }).collect(Collectors.toList());

        return R.success(dishDtos);


    }
}
